package acus.uach.rls90.calculator;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MetodosSql {
	public static final String KEY_ROWID="_id";
	public static final String KEY_FECHA="fecha";
	public static final String KEY_DCENTRO="dcentro";
	public static final String KEY_NLIVIANOS="nlivianos";
	public static final String KEY_NPESADOS="npesados";
	public static final String KEY_NMOTOS="nmotos";
	public static final String KEY_TCONTEO="tconteo";
	public static final String KEY_VELOCIDAD="velocidad";
	public static final String KEY_CROAD="croad";
	public static final String KEY_RLS90AJUSTADO="rls90ajustado";
	
	private static final String DATABASE_NAME="BBDDregistros";
	private static final String DATABASE_TABLE="ruido";
	private static final int DATABASE_VERSION=1;
	
	private DBHelper ourHelper;
	private final Context ourContext;
	private SQLiteDatabase ourDatabase;
	
	private static class DBHelper extends SQLiteOpenHelper{

		public DBHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE "+DATABASE_TABLE+" ("+
					KEY_ROWID+ " INTEGER PRIMARY KEY AUTOINCREMENT, "+
					KEY_FECHA + " TEXT NOT NULL, "+
					KEY_DCENTRO + " TEXT NOT NULL, "+
					KEY_NLIVIANOS + " TEXT NOT NULL, "+
					KEY_NPESADOS + " TEXT NOT NULL, "+
					KEY_NMOTOS + " TEXT NOT NULL, "+
					KEY_TCONTEO + " TEXT NOT NULL, "+
					KEY_VELOCIDAD + " TEXT NOT NULL, "+
					KEY_CROAD + " TEXT NOT NULL, "+
					KEY_RLS90AJUSTADO + " TEXT NOT NULL); "	);			
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS "+DATABASE_NAME);
			onCreate(db);
		}
	
	}
	public MetodosSql(Context c){
		ourContext=c;
	}
	public MetodosSql open() throws SQLException{
		ourHelper= new DBHelper(ourContext);
		ourDatabase=ourHelper.getWritableDatabase();
		return this;
	}
	
	public void close(){
		ourHelper.close();
	}
	public long createEntry(String sqlDcentro, String sqlNLivianos,
			String sqlNPesados, String sqlNMotos, String sqlTConteo,
			String sqlVelocidad, String sqlCRoad, String sqlRls90ajustado) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("DD/MM/YYYY HH:mm:ss");  
		Date date = new Date(); 
		ContentValues cv = new ContentValues();
		cv.put(KEY_FECHA, dateFormat.format(date));
		cv.put(KEY_DCENTRO, sqlDcentro);
		cv.put(KEY_NLIVIANOS, sqlNLivianos);
		cv.put(KEY_NPESADOS, sqlNPesados);
		cv.put(KEY_NMOTOS, sqlNMotos);
		cv.put(KEY_TCONTEO, sqlTConteo);
		cv.put(KEY_VELOCIDAD, sqlVelocidad);
		cv.put(KEY_CROAD, sqlCRoad);
		cv.put(KEY_RLS90AJUSTADO, sqlRls90ajustado);
		return ourDatabase.insert(DATABASE_TABLE, null, cv);
	}
}
